package com.woniu.user.service.Imp;

import com.woniu.common.CustomException.UserException;
import com.woniu.common.pojo.Flower;
import com.woniu.common.pojo.Shops;
import com.woniu.user.mapper.CommodityMapper;
import com.woniu.user.service.CommodityService;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommodityServiceImp implements CommodityService {
    @Resource
    private CommodityMapper commodityMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public List<Flower> findll(Shops shops) {
        if (shops != null) {
            List<Flower> flowersList = (List<Flower>) redisTemplate.opsForValue().get(shops.getSid() + "");
            if (flowersList != null) {
                return flowersList;
            } else {
                List<Flower> all = commodityMapper.findAll(shops);
                if (all != null) {
                    redisTemplate.opsForValue().set(shops.getSid() + "", all);
                    return all;
                }
                throw new UserException("不好意思,您的店铺没有商品了");
            }
        }
        throw new UserException("不好意思您未登陆啊");
    }

    @Override
    public void deleteFlower(Flower flower) {
        if (flower != null) {
            Integer i = commodityMapper.deleteFlower(flower);
            if (i > 0) {
                redisTemplate.delete(flower.getShops().getSid() + "");
                return;
            }
            throw new UserException("不好意思,未知异常,删除失败");
        }
        throw new UserException("不好意思,该条数据不存在");
    }

    @Override
    public void updateCommodity(Flower flower) {
        if (flower != null) {
            List<Flower> flowerByName = commodityMapper.findFlowerByName(flower);
            System.out.println(flowerByName);
            if (flowerByName == null || flowerByName.size() == 0) {
                Integer i = commodityMapper.updateCommodity(flower);
                if (i > 0) {
                    redisTemplate.delete(flower.getShops().getSid() + "");
                    return;
                } else {
                    throw new UserException("不好意思,未知异常,修改失败");
                }
            } else {
                throw new UserException("不好意思,名称重复了");
            }
        }
        throw new UserException("不好意思,该条数据不存在");
    }

    @Override
    public void addsCommodity(Flower flower) {
        if (flower != null) {
            List<Flower> flowerByName = commodityMapper.findFlowerByName(flower);
            if (flowerByName == null || flowerByName.size() == 0) {
                Integer i = commodityMapper.addsCommodity(flower);
                if (i > 0) {
                    redisTemplate.delete(flower.getShops().getSid() + "");
                    return;
                } else {
                    throw new UserException("不好意思,未知异常,修改失败");
                }
            } else {
                throw new UserException("不好意思,名称重复了");
            }
        }
        throw new UserException("不好意思,该条数据不存在");
    }

    @Override
    public List<Flower> findll() {
        //        存到redis中
        List<Flower> all = (List<Flower>) redisTemplate.opsForValue().get("all");
        if (all == null) {
            System.out.println("从数据库中获取");
            all = commodityMapper.findAllFlower();
            redisTemplate.opsForValue().set("all", all);
        } else {
            System.out.println("从redis中获取");
        }
        return all;
        //        return commodityMapper.findAllFlower();
    }

    @Override
    public List<Flower> findMidAutumn() {
        //        存到redis中
        List<Flower> midAutumn = (List<Flower>) redisTemplate.opsForValue().get("midAutumn");
        if (midAutumn == null) {
            System.out.println("从数据库中获取");
            midAutumn = commodityMapper.findMidAutumn();
            redisTemplate.opsForValue().set("midAutumn", midAutumn);
        } else {
            System.out.println("从redis中获取");
        }
        return midAutumn;
//        return commodityMapper.findMidAutumn();
    }

    @Override
    public List<Flower> findNew() {
        //        存到redis中
        List<Flower> newFlower = (List<Flower>) redisTemplate.opsForValue().get("newFlower");
        if (newFlower == null) {
            System.out.println("从数据库中获取");
            newFlower = commodityMapper.findNew();
            redisTemplate.opsForValue().set("newFlower", newFlower);
        } else {
            System.out.println("从redis中获取");
        }
        return newFlower;
//        return commodityMapper.findNew();
    }

    @Override
    public List<Flower> findLove() {
        //        存到redis中
        List<Flower> love = (List<Flower>) redisTemplate.opsForValue().get("love");
        if (love == null) {
            System.out.println("从数据库中获取");
            love = commodityMapper.findLove();
            redisTemplate.opsForValue().set("love", love);
        } else {
            System.out.println("从redis中获取");
        }
        return love;
//        return commodityMapper.findLove();
    }

    @Override
    public List<Flower> findBirthday() {
        //        存到redis中
        List<Flower> birthday = (List<Flower>) redisTemplate.opsForValue().get("birthday");
        if (birthday == null) {
            System.out.println("从数据库中获取");
            birthday = commodityMapper.findBirthday();
            redisTemplate.opsForValue().set("birthday", birthday);
        } else {
            System.out.println("从redis中获取");
        }
        return birthday;
//        return commodityMapper.findBirthday();
    }

    @Override
    public List<Flower> findType(Integer tid) {
//        //        存到redis中
//        List<Flower> type = (List<Flower>) redisTemplate.opsForValue().get("type");
//        if (type == null) {
//            System.out.println("从数据库中获取");
//            type = commodityMapper.findType(tid);
//            redisTemplate.opsForValue().set("type", type);
//            for (Flower flower : type) {
//                elasticsearchRestTemplate.save(flower, "flower");  // 确保使用小写索引名称
//            }
//        } else {
//            System.out.println("从redis中获取");
//        }
//        return type;


        return commodityMapper.findType(tid);
    }
}
